<?php
namespace Modules\Admin\Model\Data;

use Modules\Admin\Model\Data\Menu as DataMenu;
use Modules\Admin\Model\Dao\Api\Role;
/**
 * 验证及session组成
 */
class Validate {

    /**
     * 验证及session组成
     * @param $ticket
     * @param $user_info
     */
    public function validate($ticket, $user_info){
        $menu_list = $user_info['menuPrivList'];
        $action_list = array();
        foreach ($menu_list as $item){
            $menu_id = $item['menuId'];
            list(, $controller_id) =explode('_', $menu_id);
            if(!$controller_id) {
                continue;
            } else {
                $db_menu = (new DataMenu())->getMenuControlOneById($controller_id);
                $controller = strtolower($db_menu['controller']);
                $action = strtolower($db_menu['action']);
                $name = $item['menuName'];
                $action_list[$controller]['methods'][$action] = array(
                    'name' => $name,
                    'id' => $controller_id,
                    'mid' => $menu_id
                );

            }
        }
        $button_list = $user_info['buttonPrivList'];
        $permission_list = array();
        foreach ($button_list as $button_item){
            if (!array_filter($button_item['buttonIdArr'])){
                continue;
            }
            foreach ($button_item['buttonIdArr'] as $each_button_item){
                $permission_list[] = $each_button_item;
            }
        }

        $is_admin = $user_info['userPriv']['roleNameArr'][0] == Role::ROLE_ADMIN ? true : false;
        $_SESSION = array(
            'uname' => $user_info['userPriv']['username'],
            'role' => $user_info['userPriv']['roleNameArr'],
            'isadmin' => $is_admin,
            'actionlist' => $action_list,
            'permissionlist' => $permission_list,
            'ticket' => $ticket,
        );
    }

}
